package com.dony.shop.util;

import com.dony.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sundw
 * @version 1.0.0
 * @description PriceUtil
 * @since 2022/8/30 10:05
 */
@Slf4j
public class PriceUtil {


    /**
     * 通过货号获取价格
     *
     * @param name
     * @return
     */
    public static Float getPriceByName(String name) {
        if (StringUtils.isEmpty(name)) return null;
        //获取一个第一个数字
        boolean isNum = false;
        int firstIndex = 0;
        int secondIndex = name.length();
        //System.out.println("初始字符串："+name);
        for (int i = 0; i < name.length(); i++) {
            if (!isNum && Character.isDigit(name.charAt(i))) {
                isNum = true;
                firstIndex = i;
            } else if (isNum && !Character.isDigit(name.charAt(i))) {
                secondIndex = i;
                break;
            }
        }
        if (!isNum) return null;
        String postStr = name.substring(firstIndex, secondIndex);
        //如果超过1000则 为重复的价格，取最后两位
        float price = Float.parseFloat(postStr);
        if (price > 1000f) {
            postStr = name.substring(secondIndex - 2, secondIndex);
            price = Float.parseFloat(postStr);
        }
        return price;
    }


    public static Integer fomatToInteger(Float originalPrice) {
        Integer price = Math.round(originalPrice);
        //对价格进行高位取整100
        if (price < 200) {
            //price = Math.round(price / 2f) * 2;
        } else if (price > 200 && price < 500) {
            price = Math.round(price / 5f) * 5;
        } else if (price > 500 && price < 1000) {
            price = Math.round(price / 10f) * 10;
        } else if (price > 1000 && price < 5000) {
            price = Math.round(price / 20f) * 20;
        }  else if (price > 5000 && price < 10000) {
            price = Math.round(price / 50f) * 50;
        } else if (price > 10000 && price < 100000) {
            price = Math.round(price / 500f) * 500;  //这个地方可以看看要不要调整
        }else if (price > 100000) {
            price = Math.round(price / 1000f) * 1000;
        }
        //log.info("原价：{}，取整后的价格：{}", originalPrice, price);
        return price;
    }


}
